package com.itheima.service.impl.oms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.oms.OmsOrderMapper;
import com.itheima.pojo.oms.*;
import com.itheima.pojo.oms.vo.receiverInfoParam;
import com.itheima.service.oms.OmsOrderOperateHistoryService;
import com.itheima.service.oms.OmsOrderReturnApplyService;
import com.itheima.service.oms.OmsOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder>implements OmsOrderService {
    @Autowired
    private OmsOrderMapper orderMapper;
    @DubboReference
    private OmsOrderReturnApplyService omsOrderReturnApplyService;
    @DubboReference
    private OmsOrderOperateHistoryService omsOrderOperateHistoryService;

    //批量删除订单
    @Override
    public boolean delete(List<OmsOrder> ids) {
        for (OmsOrder id : ids) {
            orderMapper.deleteByIds(id);
        }
        return true;
    }
    //批量关闭订单
    @Override
    public void colse(List<Integer> ids, String note) {
        for (Integer id : ids) {
            LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(OmsOrder::getId,id);
            List<OmsOrder> list = this.list(queryWrapper);
            List<OmsOrder> orders = list.stream().map(item -> {
                item.setStatus(4);
                item.setNote(note);
                return item;
            }).collect(Collectors.toList());
            this.updateBatchById(orders);

            OmsOrderOperateHistory operateHistory = new OmsOrderOperateHistory();
            operateHistory.setOperateMan("后台管理员");
            operateHistory.setNote(note);
            operateHistory.setOrderId(id.longValue());
            operateHistory.setCreateTime(LocalDateTime.now());
            operateHistory.setOrderStatus(4);
            omsOrderOperateHistoryService.save(operateHistory);

        }

    }
    /*批量发货*/
    @Override
    public void delivery(List<deliveryParamList> deliveryParamLists) {

        for (deliveryParamList paramList : deliveryParamLists) {

            Integer orderId = paramList.getOrderId();
            String deliveryCompany = paramList.getDeliveryCompany();
            String deliverySn = paramList.getDeliverySn();
            LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OmsOrder::getId,orderId);
            List<OmsOrder> list = this.list(wrapper);
            List<OmsOrder> list1 = list.stream().map(item -> {
                item.setStatus(2);//修改物流状态
                item.setDeliveryCompany(deliveryCompany);
                item.setDeliverySn(deliverySn);
                return item;
            }).collect(Collectors.toList());
            this.updateBatchById(list1);


            OmsOrderOperateHistory operateHistory = new OmsOrderOperateHistory();
            operateHistory.setOperateMan("后台管理员");
            operateHistory.setNote("已发货");
            operateHistory.setOrderId(orderId.longValue());
            operateHistory.setCreateTime(LocalDateTime.now());
            operateHistory.setOrderStatus(2);
            omsOrderOperateHistoryService.save(operateHistory);

        }


    }
    /*修改订单费用信息*/
    @Override
    public void moneyInfo(moneyInfoParam moneyInfoParam) {
        BigDecimal discountAmount = new BigDecimal((Double) moneyInfoParam.getDiscountAmount())  ;
        BigDecimal freightAmount =   new BigDecimal((Double) moneyInfoParam.getFreightAmount());
        Integer status = moneyInfoParam.getStatus();
        Integer orderId = moneyInfoParam.getOrderId();
        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrder::getId,orderId);

        List<OmsOrder> list = this.list(wrapper);
        List<OmsOrder> list1 = list.stream().map(item -> {
           item.setDiscountAmount( discountAmount);
           item.setFreightAmount( freightAmount);
            item.setStatus(status);//修改物流状态
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);



        OmsOrderOperateHistory operateHistory = new OmsOrderOperateHistory();
        operateHistory.setOperateMan("后台管理员");
        operateHistory.setNote("修改订单费用信息");
        operateHistory.setOrderId(orderId.longValue());
        operateHistory.setCreateTime(LocalDateTime.now());
        operateHistory.setOrderStatus(status);
        omsOrderOperateHistoryService.save(operateHistory);


    }
    /*备注订单*/
    @Override
    public void note(Integer id,String note,Integer status) {
        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrder::getId,id);
        List<OmsOrder> list = this.list(wrapper);
        List<OmsOrder> list1 = list.stream().map(item -> {
            item.setNote(note);
            item.setStatus(status);//修改物流状态
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);



        OmsOrderOperateHistory operateHistory = new OmsOrderOperateHistory();
        operateHistory.setOperateMan("后台管理员");
        operateHistory.setNote(note);
        operateHistory.setOrderId(id.longValue());
        operateHistory.setCreateTime(LocalDateTime.now());
        operateHistory.setOrderStatus(status);
        omsOrderOperateHistoryService.save(operateHistory);



    }
    /*修改收货人信息*/
    @Override

        public void receiverInfo(receiverInfoParam receiverInfoParam) {



        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrder::getId,receiverInfoParam.getOrderId());
        List<OmsOrder> list = this.list(wrapper);
        List<OmsOrder> list1 = list.stream().map(item -> {
            item.setReceiverCity(receiverInfoParam.getReceiverCity());
            item.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
            item.setReceiverName(receiverInfoParam.getReceiverName());
            item.setReceiverPhone(receiverInfoParam.getReceiverPhone());
            item.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
            item.setReceiverProvince(receiverInfoParam.getReceiverProvince());
            item.setReceiverRegion(receiverInfoParam.getReceiverRegion());
            item.setStatus(receiverInfoParam.getStatus());//修改物流状态
            return item;
        }).collect(Collectors.toList());
        this.updateBatchById(list1);

        OmsOrderOperateHistory operateHistory = new OmsOrderOperateHistory();
        operateHistory.setOperateMan("后台管理员");
        operateHistory.setNote("修改收货人信息");
        operateHistory.setOrderId(receiverInfoParam.getOrderId().longValue());
        operateHistory.setCreateTime(LocalDateTime.now());
        operateHistory.setOrderStatus(receiverInfoParam.getStatus());
        omsOrderOperateHistoryService.save(operateHistory);


    }
     //订单信息
    @Override
    public List<OmsOrder> GeiByID(int id) {
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrder::getId,id);
        List<OmsOrder> list = this.list(queryWrapper);

        return list;
    }
    /*分页查询订单*/
    @Override
    public PageInfo  listAll(int pageNum, int pageSize ,String createTime,String orderSn,
                             Integer orderType,Integer   sourceType,String receiverKeyword, Integer status) {


        Page<OmsOrder> objectPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(createTime!=null, OmsOrder::getCreateTime,createTime)
                .like(orderSn!=null,OmsOrder::getOrderSn,orderSn)
                .like(orderType!=null,OmsOrder::getOrderType,orderType)
                .like(sourceType !=null,OmsOrder::getSourceType,sourceType)
                .like(receiverKeyword!=null,OmsOrder::getReceiverName,receiverKeyword)
                .like(receiverKeyword!=null,OmsOrder::getReceiverPhone,receiverKeyword)
                .like(status!=null,OmsOrder::getStatus,status);
        queryWrapper.orderByAsc(OmsOrder::getId);
        Page<OmsOrder> page = this.page(objectPage, queryWrapper);
        List<OmsOrder> records = page.getRecords();
        int total = (int)page.getTotal();
        int current = (int)page.getCurrent();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(records);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalPage(current);
        return pageInfo;
    }

    @Override
    public OmsOrder GeiByid(int id) {
        OmsOrder omsOrder = this.getById(id);
        // OmsOrder omsOrder = orderMapper.selectById(id);
        return omsOrder;
    }
    /*删除订单逻辑*/
    @Override
    @Transactional
    public void deleteIds(List<Integer> ids) {

        //删除关联的退货申请记录
        for (Integer id : ids) {
            LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OmsOrderReturnApply::getOrderId,id);
            List<OmsOrderReturnApply> list = omsOrderReturnApplyService.list(queryWrapper);
            System.out.println("++==++  "+list);
            if (list!=null){
                list.stream().map(item->{
                    Long id1 = item.getId();
                    omsOrderReturnApplyService.removeById(id1);
                    return item;
                }).collect(Collectors.toList());
            }
            //删除操作记录
            LambdaQueryWrapper<OmsOrderOperateHistory> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(id!=null,OmsOrderOperateHistory::getOrderId,id);
            List<OmsOrderOperateHistory> list11 = omsOrderOperateHistoryService.list(wrapper1);
            if(list11!=null){
                list11.stream().map(item->{
                    Long id2 = item.getId();
                    omsOrderOperateHistoryService.removeById(id2);
                    return item;
                }).collect(Collectors.toList());
            }
        }
        //删除订单
        this.removeByIds(ids);
    }
}
